package com.jy.oms.framework.asyn.task.util;

import cn.hutool.setting.dialect.Props;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.jy.framework.boot.utils.ConfUtil;

import java.util.Properties;

/**
 * 配置适配器，适用于方舟、S11
 *
 * @author liuquan
 */
public final class ServerConfigAdapter {
    private static FlagshipConfig flagshipConfig = new FlagshipConfig();
    private static S11Config s11Config = new S11Config();

    private ServerConfigAdapter() {
    }

    public static Properties getNacosProperties() {
        if (flagshipConfig.isFlagship()) {
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, ConfUtil.getEmbeddedConfig("nacos.config.server-addr"));
            properties.put(PropertyKeyConst.NAMESPACE, ConfUtil.getEmbeddedConfig("nacos.config.namespace"));
            return properties;
        }

        if (s11Config.isS11()) {
            String env = ServerConfigAdapter.getEnv();
            Props configCenterProps = new Props("configcenter.properties");

            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, configCenterProps.getStr("jy-" + env + ".servers"));
            properties.put(PropertyKeyConst.NAMESPACE, configCenterProps.getStr("jy-" + env + ".namespace"));
            return properties;
        }

        return null;
    }

    public static String getEnv() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.getEnv();
        }

        if (s11Config.isS11()) {
            return s11Config.getEnv();
        }

        return "";
    }

    public static boolean isDev() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.isDev();
        }

        if (s11Config.isS11()) {
            return s11Config.isDev();
        }

        return false;
    }

    public static boolean isTest() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.isTest();
        }

        if (s11Config.isS11()) {
            return s11Config.isTest();
        }

        return false;
    }

    public static boolean isStg() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.isStg();
        }

        if (s11Config.isS11()) {
            return s11Config.isStg();
        }

        return false;
    }

    public static boolean isProd() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.isProd();
        }

        if (s11Config.isS11()) {
            return s11Config.isProd();
        }

        return false;
    }

    public static boolean isPressure() {
        if (flagshipConfig.isFlagship()) {
            return flagshipConfig.isPressure();
        }

        if (s11Config.isS11()) {
            return s11Config.isPressure();
        }

        return false;
    }

    private static class FlagshipConfig {
        private boolean isFlagship;

        private FlagshipConfig() {
            try {
                Class.forName("com.jy.framework.boot.utils.ConfUtil");
                isFlagship = true;
            } catch (ClassNotFoundException e) {
            }
        }

        public boolean isFlagship() {
            return isFlagship;
        }

        public String getEnv() {
            return com.jy.framework.boot.utils.ConfUtil.getEnv();
        }

        public boolean isDev() {
            return com.jy.framework.boot.utils.ConfUtil.isDev();
        }

        public boolean isTest() {
            return com.jy.framework.boot.utils.ConfUtil.isTest();
        }

        public boolean isStg() {
            return com.jy.framework.boot.utils.ConfUtil.isStg();
        }

        public boolean isProd() {
            return com.jy.framework.boot.utils.ConfUtil.isProd();
        }

        public boolean isPressure() {
            return com.jy.framework.boot.utils.ConfUtil.isPressure();
        }
    }

    private static class S11Config {
        private boolean isS11;

        private S11Config() {
            try {
                Class.forName("com.jy.scm.core.config.DisConfUtil");
                isS11 = true;
            } catch (ClassNotFoundException e) {
            }
        }

        public boolean isS11() {
            return isS11;
        }

        public String getEnv() {
            return com.jy.scm.core.config.DisConfUtil.getDisConfEnv();
        }

        public boolean isDev() {
            return com.jy.scm.core.config.DisConfUtil.isDev();
        }

        public boolean isTest() {
            return com.jy.scm.core.config.DisConfUtil.isTest();
        }

        public boolean isStg() {
            return com.jy.scm.core.config.DisConfUtil.isStg();
        }

        public boolean isProd() {
            return com.jy.scm.core.config.DisConfUtil.isProd();
        }

        public boolean isPressure() {
            return com.jy.scm.core.config.DisConfUtil.isPressure();
        }
    }
}
